package com.qf.service.impl;

import com.qf.dao.AdminDao;
import com.qf.entity.*;
import com.qf.service.AdminService;
import com.qf.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;

@Service
public class AdminServiceImpl implements AdminService {
    @Autowired
    private AdminDao adminDao;

    @Override
    public R addTeacher(Teacher teacher) {
        if (teacher != null && teacher.getName() != null) {
            if (adminDao.addTeacher(teacher) > 0) {
                return new R(0, "success", null);
            } else {
                return new R(1, "fail", null);
            }
        }
        return new R(1, "fail", null);
    }

    @Override
    public R addCourse(Course course) {
        if (course != null && course.getCourseName() != null) {
            if (adminDao.addCourse(course) > 0) {
                return new R(0, "success", null);
            } else {
                return new R(1, "fail", null);
            }
        }
        return new R(1, "fail", null);
    }

    @Override
    public R addComment(String tnumber) {

        if (tnumber != null) {
            if (adminDao.updaeTeacher(tnumber) > 0) {
                return new R(0, "success", null);
            } else {
                return new R(1, "fail", null);
            }
        }
        return new R(1, "fail", null);
    }

    @Override
    public R addExamtoo(Examtoo examtoo) {
        if (examtoo != null) {
            if (adminDao.addExamtoo(examtoo) > 0) {
                return new R(0, "success", null);
            } else {
                return new R(1, "fail", null);
            }
        }
        return new R(1, "fail", null);
    }

    @Override
    public R addStudetn(Student student) {
        if (student != null && student.getName() != null) {
            if (adminDao.addStudent(student) > 0) {
                return new R(0, "success", null);
            } else {
                return new R(1, "fail", null);
            }
        }
        return new R(1, "fail", null);
    }

    //  新增考试    J
    @Override
    public R addExam(ExamSchedule schedule) {
        if (schedule != null) {
            if (adminDao.addExam(schedule) > 0) {
                return new R(0, "success", null);
            } else {
                return new R(1, "fail", null);
            }
        }
        return new R(1, "fail", null);
    }

    //  随机分配教室  J
    @Override
    public R randomClassroom(int range) {
        if (range != 0 && range == adminDao.selectExam()) {
            Random random = new Random();

            String[] arr = new String[range];
//            通过循环 将roomId 赋值给 arr数组
            for (int i = 1; i <= range; i++) {
                arr[i - 1] = adminDao.selectClassroomId(i);
            }
//            数组转List
            List<String> rangeList = new ArrayList<>(Arrays.asList(arr));
//            通过List容量获得随机数
            for (int i = 1; i <= range; i++) {
                int index = random.nextInt(rangeList.size());
//            通过循环 将随机数 修改到数据库
                int x = Integer.parseInt(rangeList.remove(index));
                adminDao.updateClassroom(i, x);

            }
        } else {
            return new R(1, "fail", null);
        }
        return new R(0, "success", null);
    }

    //  随机分配老师  J
    @Override
    public R randomTeacher(int range) {
        if (range != 0 && range == adminDao.selectExam()) {
            Random random = new Random();

            String[] arr = new String[range];
//            通过循环 将tnumber 赋值给 arr数组
            for (int i = 1; i <= range; i++) {
                arr[i - 1] = adminDao.selectTnumber(i);
            }
//            数组转List
            List<String> rangeList = new ArrayList<>(Arrays.asList(arr));
//            通过List容量获得随机数
            for (int i = 1; i <= range; i++) {
                int index = random.nextInt(rangeList.size());
//            通过循环 将随机数 修改到数据库
                int x = Integer.parseInt(rangeList.remove(index));
                adminDao.updateTnumber(i, x);
            }
        } else {
            return new R(1, "fail", null);
        }
        return new R(0, "success", null);
    }
//随机分班
    @Override
    public R randomClass(int fromStu, int fromCls) {
//        查询无班学生数量
        int needSize = adminDao.selectStudent();
        String arr[] = new String[needSize];
        Random random = new Random();
        String classId = adminDao.selectClassIdById(fromCls + 1);
//        确定需要分班 且至少为一个班
        if (needSize != 0 & needSize > 10) {

            //        查询无班学生snumber 并传入数组arr 此处需确定序号从何开始
            for (int i = 1; i <= needSize; i++) {
                arr[i - 1] = adminDao.selectSnumber(i + fromStu);
            }
            //            数组转List
            List<String> list = new ArrayList<>(Arrays.asList(arr));

//            分两班以上
            if (arr.length > 30) {
//                分组数
                int flag = 0;
//                如果为30个人 就只分一个班 31个人分两个班
                if (arr.length % 20 <= 10) {
                    flag = arr.length / 20;
//                    需要对班级容量进行修改 修改个30
                } else {
                    flag = arr.length /20 + 1;
                }
                //获取到没有分配班级的ClassId 需要多少个班 且放入数组中使用
                String arr1[] = new String[flag];
                for (int x = 1; x <= flag; x ++) {
                    arr1[x - 1] = adminDao.selectClassIdById(x + fromCls);
                }

//                外层循环 分组数
                for (int y = 1; y <= flag; y++) {

//                    内层循环 每个分组的人数 最少20 最多 30 通过分组人数需要对班级容量进行更改
                    for (int z = 1; z <=20; z++) {
                        //若为41人 分组容量为6 分7组 此时内层for循环共执行42次 会出现错误 因list中共41人
                        if (list.size() <= 0) {
                            break;
                        }
                        //            通过List容量获得随机数
                        int index = random.nextInt(list.size());
//                        获取后从list中删除学生snumber 为上次random获取的
                        String snumber = list.remove(index);
//                        将本次snumber更新 classId  问题出现
                        adminDao.updateClassId(snumber, arr1[y-1]);
                    }
                }
//                更新班级信息 已分班
                for (int d = 1; d <= flag; d++) {
                    adminDao.updateClassStatus(arr1[d-1]);
                }
                return new R(0, "分班成功且结果为" + flag + "个班", null);
            } else {
//                分一个班
                for (int a = 1; a <= arr.length; a++) {
                    int index = random.nextInt(list.size());
                    String snumber = list.remove(index);
                    adminDao.updateClassId(snumber, classId);
                }
                //                更新班级信息 已分班
                adminDao.updateClassStatus(classId);
                return new R(0, "分班成功且结果为分一个班", null);
            }
        }
        return new R(0, "分班失败", null);

    }





    //学生信息表 k
    @Override
    public List<Student> listStudent() {
        List<Student> list = adminDao.listStudent();
        for (Student student : list) {
            System.err.println(student);
        }
        return list;
    }

    //修改学生信息 k
    @Override
    public R updateStudentById(Student student) {
        if (adminDao.updateStudentById(student)>0) {
            return new R(0, "ok", "修改成功");
        }else {
            return new R(1, "fail", "修改失败");
        }
    }

    //删除学生 k
    @Override
    public boolean deleteStudent(int id) {
        return adminDao.deleteStudent(id);
    }

    //教师信息表 k
    @Override
    public List<Teacher> listTeacher() {
        List<Teacher> list1 = adminDao.listTeacher();
        for (Teacher teacher : list1) {
            System.err.println(teacher);
        }
        return list1;
    }

    //修改教师信息 k
    @Override
    public R updateTeacherById(Teacher teacher) {
        if (adminDao.updateTeacherById(teacher)>0) {
            return new R(0, "success", "修改成功");
        } else {
            return new R(1, "fail", "修改失败");
        }
    }

    //删除教师  k
    @Override
    public boolean deleteTeacher(int id) {
        return adminDao.deleteTeacher(id);
    }

    //管理员查看课程表  k
    @Override
    public List<Course> listCourse() {
        List<Course> list2 = adminDao.listCourse();
        for (Course course : list2) {
            System.err.println(course);
        }
        return list2;

    }
    //修改课程信息  k
    @Override
    public R updateCourseById(Course course) {
        if (adminDao.updateCourseById(course)>0) {
            return new R(0, "success", "修改成功");
        } else {
            return new R(0, "fail", "修改失败");
        }
    }

    @Override
    public boolean deleteCourse(int id) {
        return adminDao.deleteCourse(id);
    }

    @Override
    public Student queryStudentById(int id) {
        return adminDao.queryStudentById(id);
    }

    @Override
    public Teacher queryTeacherById(int id) {
        return adminDao.queryTeacherById(id);
    }

    @Override
    public Course queryCourseById(int id) {
        return adminDao.queryCourseById(id);
    }


}
